<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once (dirname ( __FILE__ ) . "/../../PHPExcel/PHPExcel.php");
class Supply extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回进货单列表
	 */
	public function index() {
		$name = $this->uri->segment(4);
    	$name=urldecode($name);//转化为汉字
		
    	if(empty($name)){//这里是为了将name加到url上,虽然前台也有处理，但是也有没有参数进入该方法的时候
    		$name="empty";
    	}
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=10;//暂定为10条
		
		$config['base_url']=site_url('supply/index/name/'.$name);
		$this->load->model ( 'supply_model' );
		
		if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
		// 数据总数
		$config ['total_rows'] = $this->supply_model->count_supplys($name);
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =5;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(5));
		$data ['supplys'] = $this->supply_model->find_all_supplys ($per_page,$offset,$name);
		$data ['title'] = '进货单管理 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'supply/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加进货单页面
	 */
	public function add() {
		$data ['title'] = '添加进货单 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'supply/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑进货单页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'supply_model' );
		$data ['supply'] = $this->supply_model->load ( $id );
		$data ['title'] = '编辑进货单 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'supply/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存进货单信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$content=explode('-',$name);
		$productid=$content[1];
		$cname=$content[0];
		$price = $this->input->post('price');
		$amount = $this->input->post('amount');
		$unit = $this->input->post('unit');
		$total = $this->input->post('total');
		if(empty($total)){
			$total=$price*$amount;
		}
		$buytime = $this->input->post('buytime');
		$note = $this->input->post('note');
		$supply = array (
			    'productid' => $productid,
				'name' => $cname,
				'price' => $price, 
				'amount' => $amount,
				'unit' => $unit, 
				'total' => $total,
				'supply_time' => $buytime,
				'note' => $note, 
		);
		$this->load->model ( 'supply_model' );
		if ($id) {
			if($this->supply_model->update ( $id,$supply )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未改动！'); 
			}
		}else{
			$supplyid=$this->supply_model->create ( $supply );
			if(!empty($supplyid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除进货单信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'supply_model' );
		if ($this->supply_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 * 推迟进货时间
	 */
	public function delay() {
	    $id = $this->input->post('id');
		$this->load->model ( 'supply_model' );
		$supply= $this->supply_model->load ( $id );
		$date=$supply['supply_time'];
		$newsupply = array (
				'supply_time' => date ( "Y-m-d", strtotime ( "$date +1 day" ))
		);
		if($this->supply_model->update ( $id,$newsupply )){
			return_json(TRUE,'','推迟成功！'); 
		}else{
			return_json(FALSE,'','推迟失败！'); 
		}
    }
	/*
	 * 根据日期导出进货单
	 */
	public function exportSupplys() {
		$date = $this->uri->segment(4);
		$resultPHPExcel = new PHPExcel ();
		$objActSheet = $resultPHPExcel->getActiveSheet();
		$objActSheet->setCellValue ( 'A1', '日期：'.date ( "Y-m-d", strtotime ($date)));
		$objActSheet->setCellValue ( 'A2', '商品名称' );
		$objActSheet->mergeCells('A2:B2');
		$objActSheet->setCellValue ( 'C2', '数量' );
		$objActSheet->setCellValue ( 'D2', '单价' );
		$objActSheet->setCellValue ( 'E2', '总价' );
		$objActSheet->setCellValue ( 'F2', '备注' );
		$objActSheet->setCellValue ( 'G2', '售量' );
		$this->load->model ( 'supply_model' );
		$this->load->model ( 'product_model' );
		$this->load->model ( 'orderdetail_model' );
		$date=date ( "Y-m-d", strtotime($date));
		$supplys = $this->supply_model->find_export_supplys ($date);
		$i = 3;
		$total=0;
		if(!empty($supplys)){
			foreach ( $supplys as $supply ) {
				$product = $this->product_model->load($supply['productid']);
				$objActSheet->getRowDimension($i)->setRowHeight(25);
				if(!empty($product['description'])){
					$objActSheet->setCellValue ( 'A' . $i, $supply ['name'].'-'.$product['description']);
				}else{
					$objActSheet->setCellValue ( 'A' . $i, $supply ['name']);
				}
				$objActSheet->mergeCells('A'.$i.':B'.$i);
				$objActSheet->setCellValue ( 'C' . $i, $supply ['amount'] );
				$objActSheet->setCellValue ( 'D' . $i, $supply ['price'].'元/'.$supply ['unit'] );
				$objActSheet->setCellValue ( 'E' . $i, $supply ['total']);
				$length = strlen($supply ['note']);
				if($length > 33){//超过33截断
					$supply ['note']=substr($supply ['note'],0,33);
				}
				$objActSheet->setCellValue ( 'F' . $i, $supply ['note'] );
				$monthlyresult= $this->orderdetail_model->get_monthlyamount_by_product ($supply['productid']);
				$objActSheet->setCellValue ( 'G' . $i, $monthlyresult ['amount'] );
				$total+=$supply ['total'] ;
				$i ++;
			}
			$objActSheet->setCellValue ( 'A' . $i, '总计：');
			$objActSheet->setCellValue ( 'E' . $i, $total);
			$styleArray = array(//加入边框
					'borders' => array(
							'allborders' => array(
									'style' => PHPExcel_Style_Border::BORDER_THIN
							),
					),
			);
			$objActSheet->getStyle('A2:G'.$i)->applyFromArray($styleArray);
			
			//左对齐
			$objActSheet->getStyle ( 'A1:G' . $i)->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_LEFT );
			//垂直居中
			$objActSheet->getStyle ( 'A1:G' .$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		}
		// 设置列宽
		$objActSheet->getColumnDimension ( 'A' )->setWidth ( 20 );
		$objActSheet->getColumnDimension ( 'B' )->setWidth ( 17 );
		$objActSheet->getColumnDimension ( 'C' )->setWidth ( 5 );
		$objActSheet->getColumnDimension ( 'D' )->setWidth (10);
		$objActSheet->getColumnDimension ( 'E' )->setWidth ( 8 );
		$objActSheet->getColumnDimension ( 'F' )->setWidth ( 20);
		$objActSheet->getColumnDimension ( 'G' )->setWidth ( 5);
		$outputFileName = '进货单.xls';
		$xlsWriter = new PHPExcel_Writer_Excel5 ( $resultPHPExcel );
		// ob_start(); ob_flush();
		header ( "Content-Type: application/force-download" );
		header ( "Content-Type: application/octet-stream" );
		header ( "Content-Type: application/download" );
		header ( 'Content-Disposition:inline;filename="' . $outputFileName . '"' );
		header ( "Content-Transfer-Encoding: binary" );
		header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
		header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		header ( "Pragma: no-cache" );
		$xlsWriter->save ( "php://output" );
	}
}